define(["ecp.utils","ecp.network"], function(utils, nw) {
	function network() {
		this.monitor = this;
		/**  
		 * 监听cookie.
		 * @param {Function} networkInterval  
		 */
		this.speedInterval = null;
		/**
		 * 周期时间
		 */
		this.time = 5000;		
	};
	/**
	 * 刷新颜色状态.
	 * @param index 序号
	 * @param speed 速度
	 */
	network.prototype.refreshState = function() {
		var index, speed, width;
		if(sessionStorage) {
			index = sessionStorage.getItem("NetWorkTestState");
			speed = sessionStorage.getItem("NetWorkTestSpeed");
			width = sessionStorage.getItem("NetWorkTestWidth");
		} else {
			index = utils.getCookie("NetWorkTestState");
			speed = utils.getCookie("NetWorkTestSpeed");
			width = utils.getCookie("NetWorkTestWidth");
		}
		
		if(index == null) {
			index = 2;
		}
		index = parseInt(index, 10);
		var stb = this.speedTab.rows[0];
		stb.cells[0].style.backgroundColor = "#00FF00";
		stb.cells[1].style.backgroundColor = "#FFFF00";
		stb.cells[2].style.backgroundColor = "#FF0000";
		var oldColor = ["#7AB97A","#ADAF73","#B1787A"];		
		var text = "";
		for(var i = 0; i < 3; i++) {
			if(i !== index) {
				stb.cells[i].style.backgroundColor = oldColor[i];
			}		
		}
		this.speedTab.rows[1].cells[0].innerHTML = speed;
		this.speedTab.rows[2].cells[0].innerHTML = width;
	}
	/**
	 * 初始化显示窗口.
	 */
	network.prototype.initShow = function(dom) {
		var speedDiv = document.getElementById("networktest");
		if(speedDiv == null) {
			speedDiv = document.createElement("DIV");
			speedDiv.id = "networktest";
			speedDiv.style.position = "absolute";
			speedDiv.style.top = "0px";
			speedDiv.style.right = "20px";
			speedDiv.style.height = "20px";
			speedDiv.style.width = "70px";
			document.body.appendChild(speedDiv);
			var speedTab = document.createElement("table");
			var tr = speedTab.insertRow(0);
			var k = 0;
			var greatTD = tr.insertCell(k++);
			greatTD.id = "speedgreat";
			greatTD.style.border = "1px solid #777777";
			greatTD.style.height = "4px";
			greatTD.style.width = "18px";
			var mediumTD = tr.insertCell(k++);
			mediumTD.id = "speedmedium";
			mediumTD.style.border = "1px solid #777777";
			mediumTD.style.height = "4px";
			mediumTD.style.width = "18px";
			
			var badTD = tr.insertCell(k++);
			badTD.id = "speedbad";
			badTD.style.border = "1px solid #777777";
			badTD.style.height = "4px";
			badTD.style.width = "18px";
			
			tr = speedTab.insertRow(1);
			var speedTD = tr.insertCell(0);
			speedTD.setAttribute("colSpan", 3);
			speedTD.style.height = "14px";		
			speedTD.style.textAlign = "center";	
			speedTD.id = "speedValue";
			
			tr = speedTab.insertRow(2);
			var widthTD = tr.insertCell(0);
			widthTD.setAttribute("colSpan", 3);
			widthTD.style.height = "14px";		
			widthTD.style.textAlign = "center";	
			widthTD.id = "widthValue";
			
			speedDiv.appendChild(speedTab);
		}
		this.speedDiv = speedDiv;
		this.speedTab = this.speedDiv.childNodes[0];
		if(dom != null) {
			dom.appendChild(speedDiv);
		}
	}

	/**  
	 * 开启速度监测, 实时监控.  
	 * @private  
	 */
	network.prototype.startSpeed = function() {
		window.clearInterval(this.speedInterval);
		var _this = this; 
		//先测试一回
		this.refreshState();
		//后续测试
		this.speedInterval = window.setInterval(function() {_this.refreshState();}, _this.time);
	};
	/**  
	 * 停止速度监测  
	 * @private  
	 */
	network.prototype.endSpeed = function() {
		window.clearInterval(this.speedInterval);
		this.speedInterval = null;
	};

	/**
	 * 网络监控开始
	 */
	network.prototype.start = function(dom) {
		this.initShow(dom);		
		this.startSpeed();
		//调用线程对象. 这个接口可以供外部循环调用
		//var url = null;
		//nw.sendPerformance(url);
		nw.start();
	};
	/**  
	 * 停止网络监控  
	 */
	network.prototype.stop = function() {
		this.endSpeed();
	};
	//自动创建.
	if(window._network_show == null) {
		window._network_show = new network();
	} else {
		window._network_show.stop();
	}
	return window._network_show;
});